<?php

/**
 * CelebrioMail Hotmail
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    CelebrioMail
 */
namespace ApplicationsModule\MailModule\Providers;

use \Nette\Object;
use \ApplicationsModule\MailModule\MailServer;
use \ApplicationsModule\MailModule\MailReceiver;

/**
 * Hotmail - mail provider class
 *
 * @author Celebrio Software
 */
class Hotmail extends Object implements IProvider {
    
    /* @var MailServer */
    private $mailServer;
    
    /**
     * Mail Server entity
     *
     * @author Celebrio Software
     * @param string $address (e.g. my@hotmail.com)
     * @param string $password (e.g. M1ghty^Pa22w0rd)
     */
    public function __construct($address,$password) {
	$at_sign_pos = strpos($address, "@");
	$provider = strtolower(substr($address, $at_sign_pos+1));
	if($provider != "hotmail.com" && $provider != "hotmail.cz" && $provider != "hotmail.co.uk") throw new \InvalidStateException("Hotmail provider called on invalid e-mail address.");
	$user = strtolower(substr($address, 0, $at_sign_pos));
	$this->mailServer = new MailServer(
		NULL,
		$address,
		$password,
		$address,
		"pop3.live.com",
		"pop3",
		995,
		TRUE,
		"smtp.live.com",
		25,
		FALSE);
    }
    
    /**
     * gets MailServer object containing Hotmail entity
     * @return MailServer
     */
    public function getMailServer() {
	return $this->mailServer;
    }
    
    /**
     * checks connection using POP3 protocol, id doesn't check IMAP as it is not
     * supported by Hotmail and it doesn't check SMTP as we assume it is correct
     * @return boolean
     */
    public function checkConnection() {
	$mailReceiver = new MailReceiver($this->mailServer);
	if ($mailReceiver->connect()) {
	    $mailReceiver->close();
	    return true;
	} else {
	    return false;
	}
    }
    
}